package com.example.controller;

import com.example.entity.Supplier;
import com.example.response.Result;
import com.example.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 查询供应商数据
     */
    @GetMapping
    public Result list() {
        log.info("查询全部供应商数据");

        List<Supplier> supplierList = supplierService.list();

        return Result.success(supplierList);
    }

    /**
     * 删除供应商
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除供应商:{}", id);

        supplierService.delete(id);

        return Result.success();
    }

    /**
     * 新增供应商
     */
    @PostMapping
    public Result add(@RequestBody Supplier supplier) {
        log.info("新增供应商: {}", supplier);

       supplierService.add(supplier);

        return Result.success();
    }

    /**
     * 根据ID查询供应商
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询信息, id: {}", id);
        Supplier supplier = supplierService.getById(id);
        return Result.success(supplier);
    }

    /**
     * 更新供应商信息
     */
    @PutMapping
    public Result update(@RequestBody Supplier supplier) {
        log.info("更新供应商信息 : {}", supplier);
        supplierService.update(supplier);
        return Result.success();
    }


}
